<template>
  <swmodal :isShow="isShow" :isShowBg="isShowBg" :noText="noText" :okText="okText" :title="title" :no="no" :ok="ok" :modalClass="modalClass" :isNormalText="isNormalText" :isShowCloseBtn="isShowCloseBtn" :isCloseOnClickBg="isCloseOnClickBg">
    <div class="sw-confirm">
      <div class="body" v-html="content_"></div>
    </div>
  </swmodal>
</template>

<script type="text/javascript">
import swmodal from './index.vue'
import store from '@/store/index.js'

export default {
  data () {
    return {
      noText: '',
      okText: '',
      title: '',
      content: '',
      isShow: true,
      isShowBg: false,
      needAnim: true,
      zIndex: global.swModalZIndex,
      reopenTimer: null,
      modalClass: '',
      isNormalText: false,
      isShowCloseBtn: false
    }
  },
  computed: {
    lang () {
      return store.state.lang.lang
    },
    content_ () {
      return this.getContent('content')
    }
  },
  components: {
    swmodal
  },
  methods: {
    getContent (name) {
      if (this.isNormalText) return this[name] // 普通文本
      if (this._.isFunction(this[name])) {
        return this[name]()
      }
      if (this._.isString(this[name]) && this.lang[this[name]]) {
        return this.lang[this[name]]
      }
      return this[name]
    },
    // 如果reopen和close几乎同时进行，则会被reopen里的settimeout里的this.isShow = true覆盖，导致关闭不了，所以要清除定时器reopenTimer
    close () {
      this.reopenTimer && clearTimeout(this.reopenTimer)
      this.isShow = false
    },
    reopen (needAnim) {
      this.needAnim = false
      this.isShow = false
      this.reopenTimer = setTimeout(() => {
        if (needAnim) {
          this.needAnim = true
        } else {
          this.needAnim = false
        }
        this.isShow = true
      }, 30)
    },
    no () {
      this.close()
    },
    ok () {
      this.close()
    }
  }
}
</script>

<style>
.sw-confirm {
  width: 360px;
  border-radius: 5px;
  padding: 20px 20px 0;
  margin-bottom: 20px;
  color: #fff;
}
</style>
